package com.food.restaurantpro.activity;

import java.util.ArrayList;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.log.Log;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;
import com.food.restaurantpro.objects.CurrentTime;
import com.food.restaurantpro.objects.DishCategory;
import com.food.restaurantpro.objects.Promotion;
import com.food.restaurantpro.observers.CurrentTimeObserver;
import com.food.restaurantpro.observers.CurrentTimeObserverInterface;
import com.food.restaurantpro.observers.DishCategoryObserver;
import com.food.restaurantpro.observers.DishCategoryObserverInterface;
import com.food.restaurantpro.observers.PromotionObserver;
import com.food.restaurantpro.observers.PromotionObserverInterface;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class SplashActivity extends Activity {
	
	private SplashActivity _me = this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		// Phuong thuc unRegisterReceiver phai duoc goi trong onDestroy cua
		// activity exit App (MainActivity)
		ManagingNetwork.registerReceiver(RestaurantApp.CURRENT_CONTEXT);
		generateNotification(this, getString(R.string.app_name));
	}

	private ArrayList<Promotion> mPromotionList;
	
	private boolean isCurrentTimeObserverFinish = false;
	private boolean isPromotionObserverFinish = false;
	// neu currentTime tra ve truoc thi phai cho Promotion va DishCategory tra ve moi start
	// MainActivity va nguoc lai
	private void startMainActivity() {
		if(isCurrentTimeObserverFinish && isPromotionObserverFinish) {
			//phai set lai bang false vi updateCurrentTime() ko hieu vi sao bi goi 2 lan
			//DA BIET LY DO:
			// Chu y: Observer se post notification den tat ca cac lop da register
			// observer. Va cho nao co phuong thuc trung voi phuong thuc no tra ve thi xu ly
			// cho do, nhung cho khac ko xu ly, vi vay PHAI dat code o trong "case method:"
			// khong duoc dat o ngoai
			isCurrentTimeObserverFinish = false;
			isPromotionObserverFinish = false;
			RestaurantApp.getInstance().downloadBitmapToCache(mPromotionList, _me);
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		executeAPI();
	}

	private CurrentTimeObserver mCurrentTimeObserver;
	private PromotionObserver mPromotionObserver;

	private void executeAPI() {
		
		mCurrentTimeObserver = new CurrentTimeObserver(new CurrentTimeObserverInterface() {
			
			@Override
			public void currentTimeCallback(CurrentTime currentTime) {
				//TODO
				isCurrentTimeObserverFinish = true;
				Log.i("SplashActivity", "updateCurrentTime()",
						"isCurrentTimeObserverFinish = "
								+ isCurrentTimeObserverFinish
								+ ", isPromotionObserverFinish = "
								+ isPromotionObserverFinish);
				startMainActivity();
				
			}
		});
		mCurrentTimeObserver.registerObserver();
		
		//----------------------------------------------------------------------------
		mPromotionObserver = new PromotionObserver(new PromotionObserverInterface() {
			
			@Override
			public void promotionListCallback(ArrayList<Promotion> promotionList,
					String messageError) {
				mPromotionList = promotionList;
				if(messageError == null) {
					isPromotionObserverFinish = true;
					Log.i("SplashActivity", "updatePromotionList()",
							"isCurrentTimeObserverFinish = "
									+ isCurrentTimeObserverFinish
									+ ", isPromotionObserverFinish = "
									+ isPromotionObserverFinish);
					startMainActivity();
				} else {
					isPromotionObserverFinish = true;
					startMainActivity();
				}
			}
		});
		mPromotionObserver.registerObserver();
		
		//----------------------------------------------------------------------
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (mCurrentTimeObserver != null) {
			mCurrentTimeObserver.unRegisterObserver();
			mCurrentTimeObserver = null;
		}
		
		if (mPromotionObserver != null) {
			mPromotionObserver.unRegisterObserver();
			mPromotionObserver = null;
		}
		
		Runtime.getRuntime().gc();
		super.onDestroy();
	}
	
	private static void generateNotification(Context context, String message) {
        int icon = R.drawable.phonatic_logo;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }
}
